﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AmyDonkey : Ship
{
    public WeaponSlot m_AdditionalWeaponSlotLeft;
    public WeaponSlot m_AdditionalWeaponSlotRight;
    public ShildFunction m_AdditionalShild;

    public GameObject WingLeft;
    public GameObject WingRight;
    public int m_FixShildEnargyMax = 1800;
    bool m_IsOpen = false;
    float MinAngle = -50;
    float MaxAngle = 50;
    float m_WingAngle=50;


    float m_DurationTimeCount = 0;
    public float m_DurationTimeMax = 12;
    protected new void Start()
    {
        base.Start();
    }

    // Update is called once per frame
    protected new void Update()
    {
        base.Update();
        if (!m_IsOpen && m_WingAngle == 0)
            return;

        if(m_IsOpen)
        {
            m_DurationTimeCount += Time.deltaTime;
            if(m_DurationTimeCount > m_DurationTimeMax)
            {
                m_DurationTimeCount = 0;
                m_IsOpen = false;
            }
        }

        WingLeft.transform.localRotation = Quaternion.AngleAxis(-m_WingAngle, new Vector3(0, 0, 1));
        WingRight.transform.localRotation = Quaternion.AngleAxis(m_WingAngle, new Vector3(0, 0, 1));
        if(m_IsOpen)
        {
            m_WingAngle = Mathf.Clamp(m_WingAngle - (Time.deltaTime * 20), MinAngle, MaxAngle);
        }
        else
        {
            m_WingAngle = Mathf.Clamp(m_WingAngle + (Time.deltaTime * 20), MinAngle, MaxAngle);
        }
        float scale = (-m_WingAngle+Mathf.Abs(MinAngle))/ (Mathf.Abs(MinAngle) + MaxAngle);
        m_AdditionalShild.transform.localScale = new Vector3(scale*4, scale*4, scale);
        if (m_WingAngle == 50)
        {
            if(m_AdditionalWeaponSlotLeft)
                m_AdditionalWeaponSlotLeft.m_FireLicense = false;
            if (m_AdditionalWeaponSlotRight)
                m_AdditionalWeaponSlotRight.m_FireLicense = false;
            m_AdditionalShild.gameObject.SetActive(false);
        }

    }
    public override bool LaunchSkill()
    {
        if (!base.LaunchSkill())
            return false;

        m_IsOpen = true;
        m_AdditionalWeaponSlotLeft.m_FireLicense = true;
        m_AdditionalWeaponSlotRight.m_FireLicense = true;
        m_AdditionalShild.gameObject.SetActive(true);
        m_AdditionalShild.m_FixedEnargy = m_FixShildEnargyMax;
        return true;
    }
    public override void OnChase()
    {
        if(SkillIsReady)
            LaunchSkill();
    }
    public override void OnFight()
    {
        if (SkillIsReady)
            LaunchSkill();
    }
    public override void OnRetreat()
    {
        if (SkillIsReady)
            LaunchSkill();
    }
}
